const express = require('express');
const app = express();

const cors = require('cors');
const expressJwt = require('express-jwt');
const config = require('./config');
const path = require('path');

const userRoute = require('./router/user');
const MatchRoute = require('./router/match');
const groupRoute=require('./router/groups');
const partnersRoute=require('./router/partners');
const questionRoute=require('./router/questions');

app.use(cors({
  allowedHeaders: ['Content-Type', 'Authorization'] // 确保 Authorization 被允许
}));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static(path.join(__dirname, '../xh-Prophoto')));


app.use(expressJwt({
  secret: config.jwtSecretKey,
  algorithms: ['HS256'],  
}).unless({ path: ['/api/login', '/api/register','/api/getCourseTypes','/api/getCoursesByType','/api/submitUserInfo'] }));


app.use('/api', userRoute);
app.use('/api', MatchRoute);
app.use('/api', groupRoute);
app.use('/api', partnersRoute)
app.use('/api', questionRoute)

app.use((err, req, res, next) => {
  console.log(err);
  if (err.name === 'UnauthorizedError') {
    res.send(
      {
        status: 401,
        message: '身份验证失败，请重新登录',
      }
    );
  } else {
    next(err);
  }
});


app.listen(3007, () => {
  console.log('server is running at http://127.0.0.1:3007');
});